/***2024-12-31 迷途小羔羊
* 用于编写塔防索敌行为
*/
namespace ghost
{
    export class B_TowerAttack extends B_FindAttack
    {           
        public exc(t:number):void
		{            
            let r:GameRole;
            let s = this;
            r = s._role;
			
			if(t - s._findAtkTargetTime < s._findAtkTargetInterval)return;			
			let target:GameRole;
			let targets:GameRole[];			
			if(r._isDead)return;
			if(r._skillList.length == 0)return;
			let skill:ISkillData;
			targets = r.getAtkTarget();
			target = targets[0];
			skill = r.getProritySkill(target);	
			if(skill == null)
				return;			
			// if(r._inSafeArea)
			// {
			// 	r.inRangeFlag = 1;
			// 	r.setAtkTarget(null);
			// 	target = null;
			// }
			// else
			// {				
			// 	if(target)
			// 	{			
			// 		if(target._inSafeArea)	
			// 		{//目标在安全区，取消当前目标
			// 			r.setAtkTarget(null);
			// 			target = null;
			// 		}					
					
			// 	}				
				if(target == null || !r.canAttack(target,skill.skillCfg.campFlag))
				{//查询最近目标靠近	
                    let arr:GameRole[] = r.findEnemy(skill);
					r.setAtkTarget(arr);
					target = r._targetRoles[0];				
				}
			// }
			
			//攻击目标
			if(target)
			{
				r.stopFindPath();		
				if(r._hasEnemyInRange)
				{
					if(!r.inBattleState)		
					{
						//从脱战进入战斗状态，重置前置cd
						// r.resetPreCD(t);
						r.inBattleState = true;
						// r.inResourceState = false;
					}
				}				
				
				if(s.attackEnemy(r._targetRoles, false))									
					return;	
                if(r._camp != CampType.CAMP_1)//塔防逻辑，我方人员不追打敌人
                {
                    r.resetTargetDir(target);
                    let toClosed:boolean;                    		
                    //前往攻击目标附近
                    let moveState:MoveState;
                    moveState = r.moveDir(r._targetAngle, s._speed, toClosed?target:null);
                    if(moveState.result < MoveResult.PASS)
                    {	
                        if(r._moveStopRole)
                        {
                            if(r._moveStopRole._camp == target._camp)
                            {						
                                //遭遇目标
                                if(r._moveStopRole == target)
                                {
                                    if(s._speed <= 1)
                                    {
                                        s._findAtkTargetTime = 0;
                                        s.behaviorOut(s, BehaviorEnum.DETOUR);									
                                    }
                                    else
                                    {//降低移动速度以确保能排除碰撞检测的干扰而更小步长接近目标进行攻击
                                        s._speed = Math.max(1,r.speed >> 1);
                                        r.moveDir(r._targetAngle, s._speed, toClosed?target:null);
                                        if(s.attackEnemy(r._targetRoles, false))
                                            return;	
                                    }							
                                    return;
                                }
                                //遭遇的是新目标,标记为目标
                                r.setAtkTarget([r._moveStopRole]);
                                return;
                            }						
                        }					
                        //行走遭遇障碍，设置AI停顿
                        if(s._findAtkTargetTime > 0)
                        {
                            s._findAtkTargetTime = 0;
                            s.behaviorOut(s, BehaviorEnum.DETOUR);
                            return;
                        }
                        s._findAtkTargetTime = t;
                        return;
                    }
                    if(moveState.distanceX() < 3 && moveState.distanceY() < 3)//移动步长太短，直接绕路
                    {
                        s._findAtkTargetTime = 0;
                        s.behaviorOut(s, BehaviorEnum.DETOUR);
                        return;
                    }
                    return;
                }				
			}
			
			r._lastSkillFailList[skill.skillId] = t;//记录技能查询目标失败时间，下次延后使用			
			// let nothingToDo:boolean = (CampFlag.CAMP_ALL & r.inRangeFlag) == 0;//附近不存在可针对释放技能的敌人
			// //当前战斗状态，找不到目标
			// if(nothingToDo && r._camp == CampType.CAMP_2)//塔防进攻的怪物才有路线
			// {					
            //     s.behaviorOut(s, BehaviorEnum.TOWER_ROUTE);
			// }
			// else 
            if(!r._targetMove)
			{				
				if(r.sts != StsType.STAND)
					r.resetSts();				
			}	
        }
    }
}